﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Http.Dependencies;

namespace LCTWebsite.LctHelper
{
    public  class LctHelp
    {
        public static bool IsNull(object arg)
        {
            if (arg == null)
            {
                return true;
            }
            return false;
        }
        public static bool CheckFileType(string fileName)
        {

            string ext = Path.GetExtension(fileName);

            switch (ext.ToLower())
            {

                case ".gif":

                    return true;

                case ".png":

                    return true;

                case ".jpg":

                    return true;

                case ".jpeg":

                    return true;

                default:

                    return false;

            }


        }
        public static string layDuLieuTuConfig(string key)
        {
            return ConfigurationManager.AppSettings[key];
        }
        public static string dinhDangNgayThang(DateTime date)
        {
            return String.Format("{0:dd/MM/yyyy}", date);
        }
        public static int phanTrang(int totalRecord, int soBaiViet)
        {
            if (totalRecord % soBaiViet != 0)
            {
                return totalRecord / soBaiViet + 1;
            }
            return totalRecord / soBaiViet;
        }
        public static string[] arrString(string strchuoi)
        {
            string[] arrName = strchuoi.Split(new char[] { ';' });
            return arrName;
        }
        public static bool kiemTraChuyenKyTuSangSo(string kt)
        {
            try
            {
                int kiemTra = Convert.ToInt32(kt);
            }
            catch
            {
                return false;
            }
            return true;
        }
        public static bool kiemTraEmail(string email)
        {
            bool result = false;
            if (String.IsNullOrEmpty(email))
                return result;
            email = email.Trim();
            result = Regex.IsMatch(email, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
            return result;
        }
        public static bool KiemTraChuoiRong(string s)
        {
            if (string.IsNullOrEmpty(s))
            {
                return true;
            }
            if (s.Length == 0)
                return true;
            return false;
        }
        public static string taoChuoiSHA1(string data)
        {
            SHA1 sha1 = SHA1.Create();
            byte[] hashData = sha1.ComputeHash(Encoding.Default.GetBytes(data));
            StringBuilder returnValue = new StringBuilder();
            for (int i = 0; i < hashData.Length; i++)
            {
                returnValue.Append(hashData[i].ToString());
            }
            byte[]temp= sha1.ComputeHash(Encoding.Default.GetBytes(returnValue.ToString()));
            using (SHA1Managed ha = new SHA1Managed())
            {
                var hash = sha1.ComputeHash(temp);
                return Convert.ToBase64String(hash);
            }
        }
    }
}

